package com.lge.clock.alarmclock;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.preference.ListPreference;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import android.os.Parcelable;
import android.os.Vibrator;
public class VibrateTypeListPreference extends ListPreference implements VibrateType{
	
	private static final String TAG = "VibrateTypeListPreference";
	

	private int mClickedDialogEntryIndex;
	private CharSequence[] mEntryValues;
	private Context mContext;
	Vibrator mVib = null;
	private int mAlarmDistinctiveVibration = 0;
	
    
	public VibrateTypeListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mEntryValues = getEntryValues();
        
        mVib = (Vibrator)context.getSystemService(context.VIBRATOR_SERVICE);
       
	}
	public VibrateTypeListPreference(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onPrepareDialogBuilder(Builder builder) {
		// TODO Auto-generated method stub
		super.onPrepareDialogBuilder(builder);
		mAlarmDistinctiveVibration = Settings.System.getInt(mContext.getContentResolver(),
				DISTINCTIVE_VIBRATION_ALARM, DEFAULT_VALUE);
		Log.i(TAG, "mAlarmDistinctiveVibration : "+mAlarmDistinctiveVibration);
		builder.setNegativeButton(android.R.string.cancel, new OnClickListener(){

			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				mVib.cancel();
				
			}
			
		});
		builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				
				mVib.cancel();
				
				if (mClickedDialogEntryIndex >= 0 && mEntryValues != null) {
		            String value = mEntryValues[mClickedDialogEntryIndex].toString();
		            if (callChangeListener(value)) {
		                setValue(value);
		            }
		        }
			}
		});
			
			builder.setSingleChoiceItems(getEntries() , mAlarmDistinctiveVibration-1,  // setting app array 1,2,3,4 - alarm app array 0,1,2,3  so -1 
	                new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int which) {
	                        mClickedDialogEntryIndex = which;
	                    	if(which == 0 )
	                    	{
	                    		Log.i(TAG, "vib_patten0");
	                    		mVib.vibrate(vib_patten0, 0); // 0 limit, -1 one time 
	                    	}
	                    	else if(which == 1)
	                    	{
	                    		Log.i(TAG, "vib_patten1");
	                    		mVib.vibrate(vib_patten1, 0);
	                    	}
	                    	else if(which == 2)
	                    	{
	                    		Log.i(TAG, "vib_patten2");
	                    		mVib.vibrate(vib_patten2, 0);
	                    	}
	                    	else
	                    	{
	                    		Log.i(TAG, "vib_patten3");
	                    		mVib.vibrate(vib_patten3, 0);
	                    	}
	                        
	                    }
	        });
		
	}
	@Override
	protected Parcelable onSaveInstanceState() {
		// TODO Auto-generated method stub
		mVib.cancel();
		return super.onSaveInstanceState();
	}
	
	@Override
	protected void onDialogClosed(boolean arg0) {
		// TODO Auto-generated method stub
		mVib.cancel();
		super.onDialogClosed(arg0);
	}
		

}
